Python: fix: ChatHistoryTruncationReducer orphans TOOL role messages#13608
Python: fix: ChatHistoryTruncationReducer orphans TOOL role messages#13608giulio-leone wants to merge 1 commit intomicrosoft:mainfrom
Conversation
|
Friendly ping — CI is green and this is ready for review. Happy to address any feedback. Thanks! |
da24b50 to
095face
Compare
There was a problem hiding this comment.
Pull request overview
This PR fixes a truncation edge-case in the Python chat history reducers where truncation could leave AuthorRole.TOOL messages “orphaned” by dropping the preceding assistant message containing the associated tool_calls, which can cause OpenAI chat-completions requests to be rejected.
Changes:
- Treat any
AuthorRole.TOOLmessage as “tool-related” content when computing a safe truncation boundary. - Add a regression unit test covering TOOL-role messages that don’t carry
FunctionResultContentinitems.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
python/semantic_kernel/contents/history_reducer/chat_history_reducer_utils.py |
Expands tool-related detection to include all TOOL role messages to prevent unsafe truncation boundaries. |
python/tests/unit/contents/test_chat_history_reducer_utils.py |
Adds a regression test ensuring TOOL messages aren’t separated from their preceding tool-call assistant message during reduction. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Hi! Gentle ping — this PR is rebased, CI passes, and ready for review. Happy to address any feedback. Thanks! |
095face to
38adf27
Compare
38adf27 to
ce320d8
Compare
|
Friendly ping — CI is green, tests pass, ready for review whenever convenient. Happy to address any feedback. Thanks! 🙏 |
Summary
Fixes #12708
ChatHistoryTruncationReducer.reduce()can orphanTOOLrole messages by truncating the precedingassistantmessage that contains thetool_calls, causing OpenAI to reject the history with:Root Cause
locate_safe_reduction_index()usescontains_function_call_or_result()to detect tool-related messages during its backward scan. This function only checksmsg.itemsforFunctionCallContent/FunctionResultContentinstances.However,
TOOLrole messages can contain only text content (noFunctionResultContentinitems), which causes the backward scan to treat them as regular messages. The truncation point then lands between thetool_callsassistant message and itsTOOLresponses, orphaning the tool results.Fix
Added
AuthorRole.TOOLcheck tocontains_function_call_or_result():This ensures any
TOOLrole message is recognized as part of a tool call/result pair, regardless of whether it hasFunctionResultContentin itsitems.Test
Added
test_locate_safe_reduction_index_tool_role_without_function_result_contentthat verifies TOOL role messages with only text content are not separated from their tool call.